using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(Text))]
public class LocalizedText : MonoBehaviour
{
    private string textKey; // 对应LanguageData中的key

    private Text textComponent;

    void Start()
    {
        textComponent = GetComponent<Text>();
        textKey=gameObject.name;
        UpdateText();
    }

    void OnLanguageChanged()
    {
        UpdateText();
    }

    void UpdateText()
    {
        if (LanguageManager.Instance != null && textComponent != null)
        {
            textComponent.text = LanguageManager.Instance.GetText(textKey);
            Debug.Log(textComponent.text);
        }
    }

    // 在编辑器中方便设置key
    void OnValidate()
    {
        if (Application.isPlaying && textComponent != null)
        {
            UpdateText();
        }
    }


}